import Vue from 'vue'
import App from './App.vue'
import router from './router/router'
import store from './store';
import ElementUI from 'element-ui';
Vue.use(ElementUI);

import '../public/css/global.css'
import '../public/css/common.css'
import '../public/css/admin.css'
import '../public/css/my_style.css'
import 'element-ui/lib/theme-chalk/index.css';
import adminApi from './util/common.js';
import VueUeditorWrap from 'vue-ueditor-wrap';
Vue.component('vue-ueditor-wrap', VueUeditorWrap);

import VueAMap from 'vue-amap';
VueAMap.initAMapApiLoader({
	key: '9fc2a620f49c97eaafb13ad6939b0d89',
	plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', "AMap.Autocomplete", "AMap.Geocoder", "AMap.Geolocation"],
	v: '1.4.4',
	uiVersion: '1.0.11' // 版本号
});
Vue.use(VueAMap);

//图表
import VCharts from 'v-charts-v2';
Vue.use(VCharts);

import directive from './util/directive'
Vue.use(directive)

//定义全局变量
Vue.prototype.adminApi = adminApi;

import axios from 'axios';
// 请求基准路径
// axios.defaults.baseURL = adminApi.admin_api;

var https = axios.create({
	baseURL: adminApi.admin_api,
	timeout: 100000,
});

// 请求拦截（配置发送请求的信息）-------------------
https.interceptors.request.use(function (config) {
	// 处理请求之前的配置
	const token = sessionStorage.getItem('access-token');
	var is_login = config.url.indexOf('login') == -1 ? false : true;
	if (!is_login) {
		// 添加token到headers
		if (token) {
			config.headers.Authorization = 'Bearer ' + token;
		}
	}
	return config;
}, function (error) {
	// 请求失败的处理
	return Promise.reject(error);
});

// 响应拦截（配置请求回来的信息）-------------------
https.interceptors.response.use((response) => {
	if (response.data) {
		if (response.data.code == 401) {
			router.push('/Login');
		} else {
			return response.data
		}
	} else {
		return response.data;
	}
}, function (error) {
	// 处理响应失败
	return Promise.reject(error);
});
Vue.prototype.axios = https;

//路由守卫
router.beforeEach((to, from, next) => {
	document.title = "铁建投城发管理系统平台"
	if (to.path === '/Login') return next()
	const token = sessionStorage.getItem('access-token')
	if (!token) return next('/Login')
	next()
})

Vue.config.productionTip = false

new Vue({
	router,
	store,
	render: h => h(App)
}).$mount('#app')
